package com.xc.content.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xc.content.bo.CourseBO;
import com.xc.content.bo.CourseBaseBO;
import com.xc.content.constant.ExceptionCode;
import com.xc.content.convert.CourseBaseServiceConverter;
import com.xc.content.entity.CourseMarket;
import com.xc.content.enums.CourseBaseEnums;
import com.xc.content.exception.DatabaseException;
import com.xc.content.exception.FieldEmptyException;
import com.xc.content.exception.SystemException;
import com.xc.content.mapper.CourseBaseMapper;
import com.xc.content.entity.CourseBase;
import com.xc.content.mapper.CourseMarketMapper;
import com.xc.content.utils.ExceptionUtil;
import com.xc.content.utils.SnowflakeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.UUID;

/**
 * 课程基本信息(CourseBase)表服务实现类
 *
 * @author makejava
 * @since 2024-12-25 22:55:30
 */
@Service("courseBaseService")
@Slf4j
public class CourseBaseService {

    @Resource
    private CourseMarketMapper courseMarketMapper;
    @Resource
    private CourseBaseMapper courseBaseMapper;
    @Resource
    private ExceptionUtil exceptionUtil;

    /**
     * 查询所有课程
     *
     * @return
     */
    public List<CourseBase> findAll() {
        return courseBaseMapper.findAll();
    }

    /**
     * 分页查询所有课程
     *
     * @param pageNum  页码
     * @param pageSize 数控
     * @return
     */
    public PageInfo<CourseBase> findPageWithQuery(int pageNum, int pageSize, CourseBaseBO courseBaseBO) {

        CourseBase courseBase = CourseBaseServiceConverter.INSTANCE.courseBaseBoToEntity(courseBaseBO);
        PageHelper.startPage(pageNum, pageSize);
        List<CourseBase> pageWithQuery = courseBaseMapper.findPageWithQuery(pageNum, pageSize, courseBase);
        return new PageInfo<>(pageWithQuery);
    }

    @Transactional
    public Boolean courseBaseSave(CourseBO courseBO) {

        // 设置不允许课程名称重复
        List<CourseBase> courseBaseList = courseBaseMapper.queryByName(courseBO.getName());
        if (!CollectionUtils.isEmpty(courseBaseList)) {
            log.error("该课程名称已存在,请更换新的课程名称");
            throw new DatabaseException(ExceptionCode.FIVE_HUNDRED_CODE, "课程名称为:" + courseBO.getName() + "已存在,请更换名称提交!");
        }
        if (Objects.equals(courseBO.getCharge(), "收费")) {
            if (courseBO.getPrice() == null || courseBO.getPrice() <= 0) {
                throw new FieldEmptyException(ExceptionCode.FIVE_HUNDRED_CODE, "收费规则为收费，现价不能为空或者0");
            }
        }
        try {
            CourseBase courseBase = new CourseBase();
            BeanUtils.copyProperties(courseBO, courseBase);
            String id = SnowflakeUtil.nextId();
            courseBase.setId(id);
            courseBase.setAuditStatus(CourseBaseEnums.PENDING.getDescription());
            courseBase.setStatus(CourseBaseEnums.UNPUBLISHED.getDescription());
            courseBase.setCreateDate(new Date());
            Integer isSave = courseBaseMapper.courseBaseSave(courseBase);
            if (isSave <= 0) {
                throw new DatabaseException(ExceptionCode.FIVE_HUNDRED_CODE, "课程信息新增异常");
            }

            CourseMarket courseMarket = new CourseMarket();
            BeanUtils.copyProperties(courseBO, courseMarket);
            courseMarket.setId(id);
            int insert = courseMarketMapper.insert(courseMarket);
            if (insert <= 0) {
                throw new DatabaseException(ExceptionCode.FIVE_HUNDRED_CODE, "课程营销信息新增异常");
            }
            return true;
        } catch (SystemException e) {
            throw new SystemException(ExceptionCode.FIVE_HUNDRED_CODE, "接口内部异常", e);
        }
    }

    @Transactional
    public Boolean updateCourseBase(CourseBaseBO courseBaseBO) {
        CourseBase data = courseBaseMapper.queryById(courseBaseBO.getId());
        if (ObjectUtils.isEmpty(data)) {
            log.error("未查询到该id对于的课程！！！");
            throw new DatabaseException(ExceptionCode.FIVE_HUNDRED_CODE, "未查询到对于id为:" + courseBaseBO.getId() + "的课程信息");
        }

        Long companyId = courseBaseBO.getCompanyId();
        if (!Objects.equals(companyId, data.getCompanyId())) {
            log.error("本机构只能修改本机构的课程,无法删除其他机构的课程");
            throw new RuntimeException("本机构只能修改本机构的课程,无法删除其他机构的课程");
        }
        try {
            CourseBase courseBase = CourseBaseServiceConverter.INSTANCE.courseBaseBoToEntity(courseBaseBO);
            courseBase.setChangeDate(new Date());
            Integer isUpdate = courseBaseMapper.updateCourseBase(courseBase);
            return isUpdate > 0;
        } catch (SystemException e) {
            throw new SystemException(ExceptionCode.FIVE_HUNDRED_CODE, "接口内部异常", e);
        }
    }

    /**
     * 删除课程接口
     *
     * @param courseBaseBO
     * @return
     */
    public Boolean deletedCourseBase(CourseBaseBO courseBaseBO) {
        CourseBase data = courseBaseMapper.queryById(courseBaseBO.getId());
        if (ObjectUtils.isEmpty(data)) {
            log.error("未查询到该id对于的课程！！！");
            throw new DatabaseException(ExceptionCode.FIVE_HUNDRED_CODE, "未查询到对于id为:" + courseBaseBO.getId() + "的课程信息");
        }
        try {
            Integer isDeleted = courseBaseMapper.deletedCourseBase(courseBaseBO.getId());
            return isDeleted > 0;
        } catch (SystemException e) {
            throw new SystemException(ExceptionCode.FIVE_HUNDRED_CODE, "接口内部异常", e);
        }
    }

    /**
     * 条件查询
     *
     * @param courseBaseBO
     * @return
     */
    public List<CourseBase> ConditionalQuery(CourseBaseBO courseBaseBO) {
        if (ObjectUtils.isEmpty(courseBaseBO)) {
            log.error("courseBaseBO为空:{}", courseBaseBO);
            throw new IllegalArgumentException("courseBaseBO为空");
        }
        CourseBase courseBase = CourseBaseServiceConverter.INSTANCE.courseBaseBoToEntity(courseBaseBO);
        List<CourseBase> courseBaseList = courseBaseMapper.queryByCondition(courseBase);
        if (CollectionUtils.isEmpty(courseBaseList)) {
            log.error("未查询到符合您条件的数据！");
            throw new DatabaseException(ExceptionCode.FIVE_HUNDRED_CODE, "未查询到符合您条件的数据!");
        }
        return courseBaseList;

    }
}
